//
//  MyDocument.m
//  File Metager
//
//  Created by Aaron VonderHaar on 07-05-09.
//  Copyright __MyCompanyName__ 2007 . All rights reserved.
//

#import "MyDocument.h"
#import "Item.h"

@implementation MyDocument

- (id)init
{
    self = [super init];
    if (self) {
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
		
		items = [[NSMutableArray alloc] init];
    
    }
    return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
	
	[self addObserver:self forKeyPath:@"items" options:NSKeyValueObservingOptionOld context:NULL];
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
	NSMutableData *ret = [NSMutableData data];
	NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:ret];

	[arch encodeObject:items forKey:@"items"];
	
	[arch finishEncoding];
	[arch release];
	
    return ret;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
	NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

	NSArray *theItems = [arch decodeObjectForKey:@"items"];
	[items removeAllObjects];
	[items addObjectsFromArray:theItems];
	
	NSIndexSet *allIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[items count])];
	[items addObserver:self toObjectsAtIndexes:allIndexes forKeyPath:@"notes" options:NSKeyValueObservingOptionOld context:NULL];
	[items addObserver:self toObjectsAtIndexes:allIndexes forKeyPath:@"filename" options:NSKeyValueObservingOptionOld context:NULL];



	[arch finishDecoding];
	[arch release];

    return YES;
}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(id)context
{
	//NSLog(@" Change: %@", change);
	//NSLog(@"%@", [itemsController arrangedObjects]);
	
	if ([[change objectForKey:NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeInsertion)
	{
		NSIndexSet *indexes = [change objectForKey:NSKeyValueChangeIndexesKey];
		[items addObserver:self toObjectsAtIndexes:indexes forKeyPath:@"notes" options:NSKeyValueObservingOptionOld context:NULL];
		[items addObserver:self toObjectsAtIndexes:indexes forKeyPath:@"filename" options:NSKeyValueObservingOptionOld context:NULL];
		[self updateChangeCount:NSChangeDone];
	}
	else if ([[change objectForKey:NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeSetting)
	{
		[self updateChangeCount:NSChangeDone];
	}
	else if ([[change objectForKey:NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeRemoval)
	{
		[self updateChangeCount:NSChangeDone];
	}
}

- (NSArray*)items
{
	return items;
}

- (void)delete:(id)sender
{
	[[itemsController arrangedObjects] removeObserver:self fromObjectsAtIndexes:[itemsController selectionIndexes] forKeyPath:@"notes"];
	[[itemsController arrangedObjects] removeObserver:self fromObjectsAtIndexes:[itemsController selectionIndexes] forKeyPath:@"filename"];
	[itemsController remove:self];
}

- (BOOL)canDelete
{
	return [itemsController canRemove];
}

-(BOOL)validateMenuItem: (NSMenuItem*)aMenuItem
{
	SEL action = [aMenuItem action];
	if (action == @selector(delete:))
	{
		return [self canDelete];
	}
	return TRUE;
}

@end
